package com.fitness.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.front.convertor.CoursePlanConvertor;
import com.fitness.front.modal.course.dto.CoursePlanDto;
import com.fitness.front.modal.course.entity.CoursePlan;
import com.fitness.front.modal.course.params.CoursePlanPageQueryParams;
import com.fitness.front.modal.course.vo.CoursePlanVO;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.service.ICoursePlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/front/course-plan")
public class CoursePlanController {

    private final ICoursePlanService coursePlanService;
    private final CoursePlanConvertor coursePlanConvertor;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<CoursePlanVO>> page(CoursePlanPageQueryParams queryParams) {
        Page<CoursePlanVO> result = coursePlanService.getCoursePlanPage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param equipmentDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<Boolean> add(@RequestBody CoursePlanDto equipmentDto) {
        boolean result = coursePlanService.addCoursePlan(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("CP10001","添加课程计划失败");
    }

    /**
     * 修改
     * @param equipmentDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<Boolean> update(@RequestBody CoursePlanDto equipmentDto) {
        boolean result = coursePlanService.updateCoursePlan(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("CP10002","更新课程计划信息失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(@RequestBody Long id) {
        boolean result = coursePlanService.removeById(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("CP10003","删除课程计划信息失败");
    }

    /**
     * 获取课程计划表单
     */
    @GetMapping("/getCoursePlanForm")
    public ServerResponseEntity<CoursePlanVO> getCoursePlanForm(Long id) {
        CoursePlan result = coursePlanService.getById(id);
        return ServerResponseEntity.success(coursePlanConvertor.entity2Vo(result));
    }

    @GetMapping("/getPlansByCourseId")
    public ServerResponseEntity<List<CoursePlanVO>> getPlansByCourseId(Long id) {
        List<CoursePlanVO> result = coursePlanService.getCourPlansVOByCourseId(id);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param planDtos
     * @return
     */
    @PostMapping("/batchAdd")
    public ServerResponseEntity<Boolean> batchAdd(@RequestBody List<CoursePlanDto> planDtos) {
        boolean result = coursePlanService.batchAdd(planDtos);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("CP10001","添加课程计划失败");
    }

    @DeleteMapping("/clearPlans")
    public ServerResponseEntity<Boolean> clearPlans(@RequestBody Long id) {
        QueryWrapper<CoursePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        boolean result = coursePlanService.remove(queryWrapper);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("CP10003","删除课程计划信息失败");
    }
}
